package com.device.grant.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.device.grant.dao.DepartmentDao;
import com.device.grant.dao.IemployeeDao;
import com.device.grant.pojo.IDepartment;
import com.device.grant.pojo.IEmployee;
import com.device.grant.pojo.vo.IEmployeeVo;
import com.device.grant.service.IemployeeService;
import com.device.grant.utils.BeanHelper;
import com.device.grant.utils.ColumnCompator;

@Service("iemployeeService")
public class IemployeeServiceImpl implements IemployeeService {

    @Autowired
    private IemployeeDao iemployeeDao;

    @Autowired
    private DepartmentDao departmentDao;

    private static final Logger logger = LoggerFactory.getLogger(IemployeeServiceImpl.class);

    public boolean addIemployee(IEmployee employee) {
        return iemployeeDao.addIemployee(employee);
    }

    public Integer getQueryCount(Map<String, Object> params, List<String> blearParams, Map<String, ColumnCompator> compators) {
        return iemployeeDao.getQueryCount(params, blearParams, compators);
    }

    public List<IEmployeeVo> queryIemployees(Integer page, Integer rows, Map<String, Object> conditions,
            List<String> blearParams, Map<String, ColumnCompator> compators, String orderBy) {
        List<IEmployee> ies = iemployeeDao.queryIemployees(page, rows, conditions, blearParams, compators, orderBy);

        return adapterDept(ies);
    }

    public boolean updateIemployee(Map<String, Object> changeInfos, Map<String, Object> conditions) {
        return iemployeeDao.updateIemployee(changeInfos, conditions);
    }

    public IEmployeeVo getIemployee(Map<String, Object> conditions) {
        IEmployee ie = iemployeeDao.getIemployee(conditions);

        if (null != ie) {
            List<IEmployee> ies = new ArrayList<IEmployee>();
            ies.add(ie);

            List<IEmployeeVo> vos = adapterDept(ies);
            if (null != vos && !vos.isEmpty()) {
                return vos.get(0);
            }
        }

        return null;
    }

    private List<IEmployeeVo> adapterDept(List<IEmployee> ies) {
        List<IEmployeeVo> vos = Collections.emptyList();
        try {
            if (null != ies && !ies.isEmpty()) {
                vos = new ArrayList<IEmployeeVo>();

                Set<Integer> set = new HashSet<Integer>();
                for (IEmployee ie : ies) {
                    set.add(ie.getDeptID());
                }

                List<IDepartment> idpts = departmentDao.getDepartMentsIncludeParent(set);

                for (IEmployee ie : ies) {

                    IEmployeeVo vo = BeanHelper.getInstanceFromOrigBean2(IEmployeeVo.class, ie);

                    for (IDepartment idpt : idpts) {
                        if (ie.getDeptID().equals(idpt.getDeptID())) {
                            vo.setDeptName(idpt.getDeptName());
                        }
                    }
                    vos.add(vo);
                }
            }
        } catch (Exception e) {
            logger.error("adapterDept - adapter Department error." + ies, e);
        }

        return vos;
    }

    public List<IEmployee> queryEmployeeSInEmpNos(Set<String> empNos) {
        return iemployeeDao.queryEmployeeSInEmpNos(empNos);
    }

    public List<IEmployee> queryEmployeeSInIds(Set<Integer> ids) {
        return iemployeeDao.queryEmployeeSInIds(ids);
    }

    public boolean reviewGroupRegister(Map<String, Object> changeInfos, Map<String, Object> conditions, Integer exits) {
        return iemployeeDao.reviewGroupRegister(changeInfos, conditions, exits);
    }

}
